package com.swq.java.code.chapter20.code12;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyDialog extends JDialog {
    private JLabel label = new JLabel("请输入姓名：");
    private JTextField textField = new JTextField(10);
    private JButton button = new JButton("OK");

    public MyDialog(JFrame parent) {
        super(parent, "提示", true);
        setLayout(new FlowLayout());
        add(label);
        add(textField);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose(); // 销毁对话框
            }
        });
        add(button);
        pack();
    }

    public String getText() {
        return textField.getText();
    }
}

public class DialogDemo extends JFrame {
    private JButton button = new JButton("提交");
    private MyDialog dialog = new MyDialog(this);
    private JTextField textField = new JTextField(10);

    public DialogDemo(String title) {
        super(title);
        textField.setEditable(false);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(true);  //显示对话框
                textField.setText(dialog.getText());
            }
        });

        setLayout(new FlowLayout());
        add(textField);
        add(button);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 300);
        setVisible(true);
    }

    public static void main(String args[]) {
        new DialogDemo("DialogDemo");
    }
}
